import { createRouter, createWebHistory } from "vue-router";

const routes = [
  { path: "/", redirect: "/auth" },
  { path: "/auth", component: () => import("../views/AuthView.vue") },
  { path: "/search", component: () => import("../views/SearchView.vue") },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 全局路由守卫：鉴权
router.beforeEach((to) => {
  const apiKey = localStorage.getItem("apiKey");
  if (!apiKey && to.path !== "/auth") {
    return "/auth";
  }
  if (apiKey && to.path === "/auth") {
    return "/search";
  }
});

export default router;
